<template>
  <div class="setPermission">
    <el-dialog title="分配权限" :visible="visibleShow" @close="closeEvent">
      <el-tree
        ref="tree"
        check-strictly
        node-key="id"
        :data="treeData"
        show-checkbox
        :props="{ label: 'name', children: 'children' }"
      />
      <template #footer>
        <div style="text-align: center">
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleDetail, sysRoleAssignPrem } from '@/api/setting'
export default {
  model: {
    prop: 'visibleShow',
    event: 'setPermission'
  },
  props: {
    visibleShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: [],
      id: ''
    }
  },
  async created() {
    const res = await sysPermission()
    this.treeData = changeData(res.data, '0')
  },
  methods: {
    closeEvent() {
      this.$emit('setPermission', false)
      this.$refs.tree.setCheckedKeys([])
    },
    // 用于获取当前角色的权限数据
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleDetail(id)
      // 设置需要选中的节点数据
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('分配角色权限成功')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
